//
//  NSFileManager+TYHelper.m
//  TYFoundation
//
//  Created by 郭艺伟 on 2019/2/28.
//

#import "NSFileManager+TYHelper.h"
#include <sys/xattr.h>
@implementation NSFileManager (TYHelper)

+ (void)ty_addDoNotBackupAttribute:(NSString *)path {
    NSURL *url = [NSURL fileURLWithPath:path];
    if (!url) {
        return;
    }
    
    NSError *error = nil;
    [url setResourceValue:@YES forKey:NSURLIsExcludedFromBackupKey error:&error];
    if (error) {
        NSLog(@"error to set do not backup attribute, error = %@", error);
    }
}

@end

@implementation NSFileManager (TYImageHelper)

+ (BOOL)ty_isImageFilePath:(NSString *)path scaleType:(TYImageScaleType)type {
    return NO;
}

+ (BOOL)ty_isImageFileOnly:(NSString *)filePath scaleType:(TYImageScaleType)type {
    return NO;
}

+ (NSString *)ty_imageFilePathByFileName:(NSString *)fileName
                                extension:(NSString *)extensionName
                                scaleType:(TYImageScaleType)type {
    return nil;
}

+ (NSString *)ty_imageFilePathByName:(NSString *)name
                            scaleType:(TYImageScaleType)type {
    return nil;
}



+ (BOOL)ty_isRetinaFilePath:(NSString *)path {
    NSRange retinaSuffixRange = [[path lastPathComponent] rangeOfString:@"@2x" options:NSCaseInsensitiveSearch];
    return retinaSuffixRange.length && retinaSuffixRange.location != NSNotFound;
}

+ (BOOL)ty_isRetinaImageFileOnly:(NSString *)stringFileName {
    NSAssert(stringFileName && [stringFileName length] , @"文件名必须不为空！");
    
    NSString *extension = [stringFileName pathExtension];
    NSString *fileName = [stringFileName stringByDeletingPathExtension];
    NSAssert(extension && [extension length] , @"扩展名必须不为空！");
    
    //检测传入文件名是否已经是@2x了
    NSRange range = [fileName rangeOfString:@"@2x" options:NSCaseInsensitiveSearch];
    if (range.length && range.location != NSNotFound) {
        fileName = [fileName stringByReplacingCharactersInRange:range withString:@""];
    }
    
    NSString *pathRetina =  [NSFileManager ty_imageFilePathByFileName:fileName extension:extension isRetina:YES];
    BOOL bHasRetinaPath = [[NSFileManager defaultManager] fileExistsAtPath:pathRetina];
    
    NSString *pathCommon =  [NSFileManager ty_imageFilePathByFileName:fileName extension:extension isRetina:NO];
    BOOL bHasCommonPath = [[NSFileManager defaultManager] fileExistsAtPath:pathCommon];
    
    if (bHasRetinaPath && !bHasCommonPath) {
        return YES;
    }
    
    return NO;
}

+ (NSString *)ty_imageFilePathByFileName:(NSString *)fileName extension:(NSString *)extensionName isRetina:(BOOL)isRetina {
    if (!fileName || !extensionName ||  ![fileName length] || ![extensionName length]) {
        return nil;
    }
    
    NSRange range = [fileName rangeOfString:@"@2x" options:NSCaseInsensitiveSearch];
    if (range.length && range.location != NSNotFound) {
        fileName = [fileName stringByReplacingCharactersInRange:range withString:@""];
    }
    
    NSString *fileNameTemp = [NSString stringWithFormat:@"%@%@.%@", fileName, isRetina?@"@2x":@"", extensionName];
    NSString *path = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:fileNameTemp];
    
    return path;
}

+ (NSString *)ty_imageFilePathByName:(NSString *)name isRetina:(BOOL)isRetina {
    if (!name || ![name length]) {
        return nil;
    }
    
    NSString *extension = [name pathExtension];
    NSString *fileName = [name stringByDeletingPathExtension];
    
    return [NSFileManager ty_imageFilePathByFileName:fileName extension:extension isRetina:isRetina];
}

//对文件、文件夹设置不被iTune、iCloud备份
+ (BOOL)ty_addSkipBackupAttributeToItemAtURL:(NSString*)sPath {
    if (![[NSFileManager defaultManager] fileExistsAtPath:sPath]) {
        return NO;
    }
    
    NSURL* URL = [NSURL fileURLWithPath:sPath isDirectory:NO];
    const char* filePath = [[URL path] fileSystemRepresentation];
    const char* attrName = "com.apple.MobileBackup";
    u_int8_t attrValue = 1;
    int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    (void)attrValue;
    return result == 0;
}

//查看标识
+ (int)ty_getSkipBackupAttributeToItemAtURL:(NSString*)sPath {
    if (![[NSFileManager defaultManager] fileExistsAtPath:sPath]) {
        return 0;
    }
    
    NSURL* URL = [NSURL fileURLWithPath:sPath isDirectory:NO];
    const char* filePath = [[URL path] fileSystemRepresentation];
    const char* attrName = "com.apple.MobileBackup";
    u_int8_t attrValue = 1;
    ssize_t result = getxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
    (void)attrValue;
    return (int)result;
}

//判断是否设置了不备份属性
+ (BOOL)ty_isItemSkipBackup:(NSString*)sPath {
    return [NSFileManager getSkipBackupAttributeToItemAtURL:sPath] == 1;
}

+ (BOOL)ty_isFileExistInPath:(NSString *)stringPath {
    NSError *error = nil;
    NSFileManager *fm = [NSFileManager defaultManager];
    
    return ([fm fileExistsAtPath:stringPath] && [[fm attributesOfItemAtPath:stringPath error:&error] fileSize] > 10);
}

+ (int)getSkipBackupAttributeToItemAtURL:(NSString*)sPath {
    return 0;
}

@end
